<?php
class LoginModel extends Model{
	public $table='admin';
	public function login(){
		$username=Q('post.username');
		$password=Q('post.password');
		$code=Q('post.code','','strtoupper');
		
		if(empty($username)){
			$this->error='账号不能为空';
			return false;
		}
		if(empty($password)){
			$this->error='密码不能为空';
			return false;
		}
		if($code!==session('code')){
			$this->error='验证码输入错误...';
			return false;
		}
		$user=$this->where("username='$username'")->find();
		if(!$user){
			$this->error='账号不存在';
			return false;
		}
		if($user['password']!=md5($password.$user['code'])){
			$this->error='密码错误,请重新输入';
			return false;
		}
		unset($user['password']);//存在session里的
		unset($user['code']);
		$_SESSION['user']=$user;//SESSION读取的数据和数据库里的数据一致时,为真
		return true;
	}
}